package com.yonyou.gmcc.gateway.web.controller.authorization;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yonyou.gmcc.gateway.web.consts.CommonConstants;
import com.yonyou.gmcc.gateway.web.entity.UserEntity;
import com.yonyou.gmcc.gateway.web.jwt.JwtHelper;
import com.yonyou.gmcc.gateway.web.service.PermissionInition;
import com.yonyou.gmcc.gateway.web.util.Md5Utils;

@Controller
@RequestMapping(value = "manager")
public class AuthorizationController {

	@Autowired
	private PermissionInition permissionInition;
	
	@RequestMapping(value="login", method=RequestMethod.GET, consumes="application/json")
	public @ResponseBody String login(HttpServletRequest request, RedirectAttributes rediect) {
		String account = request.getParameter("account");
		String password = Md5Utils.getMD5(request.getParameter("password"));
		
		UsernamePasswordToken upt = new UsernamePasswordToken(account, password);
		upt.setHost("web");
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(upt);
		} catch (AuthenticationException e) {
			e.printStackTrace();
			rediect.addFlashAttribute("errorText", "您的账号或密码输入错误!");
			return "redirect:/login";
		}
		return "index";
	}

	@RequestMapping(value="reloadPermission", method=RequestMethod.GET)
	public @ResponseBody String reloadPermission(){
		permissionInition.initPermission();
		return "reaload success";
	}
	
	@RequestMapping(value = "/login.web", method = RequestMethod.POST, consumes="application/json")
	public @ResponseBody String login(@RequestBody UserEntity user,HttpServletRequest req,
			HttpServletResponse response){ 
		System.out.println("--------login");
		//这里验证用户的密码
		user.setUserId("comeFromDb");
		String token=JwtHelper.createJWT(user.getUserName(), user.getUserId(), "", "", "", 30);
		this.writeCookie(token, req, response);
		return token;
	}
	@RequestMapping(value = "/wxLogin", method = RequestMethod.GET)
	public String  wxLogin(Map<String,Object> map ,HttpServletRequest req,
			HttpServletResponse response){ 
		UserEntity user=new UserEntity();
		user.setUserName("testWx");
		user.setUserId("comeFromDb");
		//这里验证用户的密码
		String token=JwtHelper.createJWT(user.getUserName(), user.getUserId(), "", "", "", 30);
		System.out.println("--------login,token="+token);
		map.put("token", token);
		return "/wxIndex";
	}
	@RequestMapping(value = "/helloHtml", method = RequestMethod.GET)  
    public String helloHtml(Map<String,Object> map){  
  
       map.put("hello","from TemplateController.helloHtml");  
       return"/wxIndex";  
    } 
	private void writeCookie(String token,HttpServletRequest req,
			HttpServletResponse response){
		response.setCharacterEncoding("GBK");
		response.setContentType("text/html;charset=UTF-8");
		Cookie cookie; 
		cookie=new Cookie(CommonConstants.CHAT_COOKIE_NAME,token);//用户ID 
		cookie.setMaxAge(60*60);//cookie时间 
		cookie.setPath(req.getContextPath()); //根据个人的不用，在不同功能的路径下创建 
		response.addCookie(cookie); 
	}
	@RequestMapping(value = "/test", method = RequestMethod.POST)
	public @ResponseBody String test(HttpServletRequest req,
			HttpServletResponse response){
		
		return "{\"a\":\"name\"}";
	}
}
